package com.systop.scos.diary.webapp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.MatchMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.systop.common.modules.security.user.LoginUserService;
import com.systop.common.modules.security.user.model.User;
import com.systop.core.dao.support.Page;
import com.systop.core.util.DateUtil;
import com.systop.core.util.PageUtil;
import com.systop.core.webapp.struts2.action.DefaultCrudAction;
import com.systop.scos.diary.diaryType.model.DiaryType;
import com.systop.scos.diary.diaryType.service.DiaryTypeManager;
import com.systop.scos.diary.model.Diary;
import com.systop.scos.diary.service.DiaryManager;

/**
 * 日志action
 * 
 */
@SuppressWarnings({ "serial", "unchecked", "rawtypes" })
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class DiaryAction extends DefaultCrudAction<Diary, DiaryManager> {

	/**
	 * 日志类别管理
	 */
	@Autowired
	private DiaryTypeManager diaryTypeManager;

	/**
	 * 查询起始时间
	 */
	private Date beginTime;

	/**
	 * 查询截至时间
	 */
	private Date endTime;

	/**
	 * 登陆用户信息管理
	 */
	@Autowired
	private LoginUserService loginUserService;

	/**
	 * 保存日志
	 */
	@Override
	public String save() {
		if (getModel().getDiaryType() != null
				&& getModel().getDiaryType().getId() != null) {
			DiaryType diaryType = getManager().getDao().get(DiaryType.class,
					getModel().getDiaryType().getId());

			getModel().setDiaryType(diaryType);
		}

		getModel().setEndDate(DateUtil.getCurrentDate());
		getModel().setUser(loginUserService.getLoginUser(getRequest()));
		getManager().getDao().clear();
		getManager().save(getModel());
		return SUCCESS;
	}

	/**
	 * 日志查询列表
	 */
	@Override
	public String index() {
		Page page = PageUtil.getPage(getPageNo(), getPageSize());
		StringBuffer sql = new StringBuffer("from Diary d where 1=1 ");
		List args = new ArrayList();
		User user = loginUserService.getLoginUser(getRequest());
		if (user != null) {
			sql.append(" and d.user.id = ?");
			args.add(user.getId());
			if (StringUtils.isNotBlank(getModel().getTitle())) {
				sql.append(" and d.title like ?");
				args.add(MatchMode.ANYWHERE
						.toMatchString(getModel().getTitle()));
			}

			if (getModel().getDiaryType() != null
					&& getModel().getDiaryType().getId() != null) {
				sql.append(" and d.diaryType.id =?");
				args.add(getModel().getDiaryType().getId());

			}
			if (beginTime != null && endTime != null) {
				sql.append(" and d.createDate >= ? and d.createDate <= ? ");
				args.add(beginTime);
				args.add(endTime);
			}
			sql.append(" order by d.createDate desc");
			page = getManager().pageQuery(page, sql.toString(), args.toArray());
			restorePageData(page);
		}
		return INDEX;
	}

	/**
	 * 查看日志详细信息
	 */
	@Override
	public String view() {
		return super.view();
	}

	/**
	 * 删除日志及日志附件信息
	 */
	@Override
	public String remove() {
		if (getModel().getId() != null) {
			getManager().remove(getModel());
		}
		return SUCCESS;
	}

	/**
	 * 显示类别
	 */
	public Map getDiaryTypeMap() {
		return diaryTypeManager.getDiaryTypeMap();
	}

	public Date getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(Date beginTime) {
		this.beginTime = beginTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

}
